package com.souku.finance.application.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * Created by lenovo on 2016/6/11.
 */
public class MyTextUtil {

    /**
     * 检测手机号是否合法
     *
     * @param tel 手机号
     * @return
     */
    public static boolean isTelephone(String tel) {
        String regex = "^1[34578]\\d{9}$";
        return Pattern.matches(regex, tel);
    }

    /**
     * 手机号转****  example：151****4360
     *
     * @param phone 手机号
     * @return
     */
    public static String phoneToPass(String phone) {
        if (phone == null) return "";
        if (phone.length() < 5) return phone;
        int num = phone.length();
        return phone.substring(0, 3) + "****" + phone.substring(num - 4, num);
    }

    /**
     * 名字转*  example：王*
     *
     * @param name
     * @return
     */
    public static String nameToPass(String name) {
        if (name == null) return "";
        int num = name.length();
        if (num == 2) {
            return "*" + name.substring(1, num);
        } else if (num == 3) {
            return "*" + name.substring(1, num);
        } else if (num > 3) {
            return "**" + name.substring(2, num);
        } else {
            return "";
        }
    }

    public static String countParse(Object count) {
        if (count == null) {
            return "0";
        } else {
            return count + "";
        }
    }

    /**
     * 银行卡转****
     *
     * @param bankNo
     * @return
     */
    public static String bankNoToPass(String bankNo) {
        if (bankNo.isEmpty()) {
            return "";
        } else if (bankNo.length() == 0) {
            return "";
        } else {
            int num = bankNo.length();
            return bankNo.substring(0, 4) + "****" + bankNo.substring(num - 3, num);
        }
    }

    public static String bankNoToPasslast4(String bankNo) {
        if (bankNo == null) {
            return "";
        } else if (bankNo.length() == 0) {
            return "";
        } else {
            int num = bankNo.length();
            return bankNo.substring(num - 4, num);
        }
    }


    /**
     * 毫秒数转字符串时间 类型：yyyy-MM-dd
     *
     * @param timesecret
     * @return
     */
    public static String changeTimeFormat(String timesecret) {
        return timesecret;
    }

    /**
     * 毫秒数转字符串时间 类型：yyyy-MM-dd
     *
     * @param timesecret
     * @return
     */
    public static String changeTimeFormat2(String timesecret) {
        if (timesecret == null) {
            return "";
        } else if (timesecret.length() == 0) {
            return "";
        } else if (timesecret.length() > 10) {
            long time = Long.parseLong(timesecret);
            String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(time));
            return date;
        } else {
            long time = Long.parseLong(timesecret);
            String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(time));
            return date;
        }
    }

    public static String getSysTimeString() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    public static String changeTimeWithHM(String timesecret) {
        if (timesecret == null) {
            return "";
        } else if (timesecret.length() == 0) {
            return "";
        } else if (timesecret.length() > 10) {
            long time = Long.parseLong(timesecret);
            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(time));
            return date;
        } else {
            long time = Long.parseLong(timesecret);
            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(time * 1000));
            return date;
        }
    }

    /**
     * 设置字体颜色
     */
    public static SpannableString setFontColor(String string, int start, int end, int color) {
        SpannableString ss = new SpannableString(string);
        ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }


    public static String getValue(String num) {
        NumberFormat fomatter = new DecimalFormat();
        String[] dataList = fomatter.format(
                Double.parseDouble(String.valueOf(num))).split(",");
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < dataList.length; i++) {
            sb.append(dataList[i]);
        }
        return sb.toString();
    }

    /**
     * 判断空值
     *
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        if (null == s)
            return true;
        if (s.length() == 0)
            return true;
        if (s.trim().length() == 0)
            return true;
        if (s.equals("[]"))
            return true;
        return false;
    }

    /**
     * double类型保留两位小数
     *
     * @param db
     * @return
     */
    public static String doubleFormat(Double db) {
        DecimalFormat df = new DecimalFormat("#0.00");
        return df.format(db);
    }

    /**
     * double类型保留一位小数
     *
     * @param db
     * @return
     */
    public static String doubleFormat1(Double db) {
        DecimalFormat df = new DecimalFormat("#0.0");
        return df.format(db);
    }

    /**
     * 判断设备号是否合法
     *
     * @param dev
     * @return
     */
    public static boolean isDevNum(String dev) {
        String regex = "^0+$";
        return Pattern.matches(regex, dev);
    }

    /**
     * 科学计数法转
     *
     * @param
     * @return
     */
    public static String getStringOutE(String str) {
        BigDecimal bd = new BigDecimal(str);
        return bd.toPlainString();
    }

    public static String getStringWithoutIsAPP(String url) {
        if (url.endsWith("isAPP=1&")) {
            return url.replace("isAPP=1&", "isAPP=0");
        } else if (url.contains("isAPP=1&")) {
            return url.replace("isAPP=1&", "isAPP=0&");
        } else if (url.contains("?")) {
            return url + "&isAPP=0";
        } else {
            return url + "?isAPP=0";
        }
    }

    public static void setType(Context context, TextView tv) {
        Typeface fontFace = Typeface.createFromAsset(context.getAssets(),
                "DINCond-Regular.otf");
        tv.setTypeface(fontFace);
    }

    public static String setMoneyToElement(String moneyBranch) {
        if (moneyBranch == null) {
            return "0";
        }
        return doubleFormat(Double.parseDouble(moneyBranch) / 100);
    }

    public static double calculationMoney(int day, double rate, int money) {
        return day * money * rate / 365;
    }
}
